#!/usr/bin/env python
# -*- coding:utf-8 -*-


'''
@Author : shouke
'''

from components.decoraters.action_decorater import ActionDecorator

class OnceOnlyController(object):
    @ActionDecorator.action_register('controll_execution_once_only', 'OnceOnlyController', 'controll_execution_only_once', __name__)
    @classmethod
    def controll_execution_only_once(self, config:dict, *args, **kwargs):
        '''
        控制仅运行一次
        :param config 配置信息，具体如下
        {
            "id": 5,# 配置唯一标识，代表该函数,如果需要重复使用该组件，要求每次传入的id值都不同，且唯一
            "children": [{
                # 略，其它action
                }, {...}]
            }
        '''

        user = kwargs['user']
        user_resource_dict = user.user_resource_dict[user.chain_id]
        if config['id'] not in user_resource_dict['func_exec_times']:
            user_resource_dict['func_exec_times'][config['id']] = 1
            if config.get('children'):
                kwargs['user'].run_actions(config['children'])